package com.seio.edc.controller.system;

import com.nova.edc.model.system.AreaPoint;
import com.nova.edc.model.system.CustomerArea;
import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.service.system.AreaPointService;
import com.nova.edc.service.system.CustomerAreaService;
import com.nova.edc.service.system.PointAirService;
import com.nova.edc.service.system.PointLightService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.seio.edc.util.DateTimeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/systemCustomerArea")
public class CustomerAreaContronller {
    private static Logger log = Logger.getLogger(CustomerAreaContronller.class);

    @Autowired
    private PointLightService pointLightService;

    @Autowired
    private PointAirService pointAirService;

    @Autowired
    private AreaPointService novaAreaPointService;

    @Autowired
    private com.nova.edc.service.system.CustomerInfoService novaCustomerInfoService;

    @Autowired
    private CustomerAreaService novaCustomerAreaService;

    private static final int PAGE_SIZE = 10;

    /**
     * 初始化区域管理
     */
    @RequestMapping(value = "customerAreaList")
    public ModelAndView customerAreaList(HttpServletRequest request,
                                         HttpSession session, Integer pageIndex, CustomerArea customerArea, ModelMap modelMap) {
        log.info("初始化区域管理开始" + DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD_HH_MM_SS));
        CustomerInfo customerInfo = novaCustomerInfoService.getById(customerArea.getCustomerinfoid());
        Page page = new Page();
        page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
        page.setPageSize(PAGE_SIZE);
        modelMap.put("pageIndex", page.getCurrentPage());
        modelMap.put("customerArea", customerArea);
        PageList<CustomerArea> customerAreas = novaCustomerAreaService
                .findPageCustomerAreaDetail(customerArea, page, "id", "desc");
        modelMap.put("customerAreas", customerAreas.getDatas());
        modelMap.put("dataCount", page.getRowCount());
        modelMap.put("pageCount", page.getTotalPage());
        modelMap.put("customerInfo", customerInfo);

        return new ModelAndView("system/customerAreaList", modelMap);
    }

    /**
     * 添加新区域
     */
    @RequestMapping(value = "customerAreaEdit")
    public ModelAndView customerAreaEdit(HttpServletRequest request,
                                         HttpSession session, Integer id, Integer customerinfoid, ModelMap modelMap) {
        modelMap.put("customerinfoid", customerinfoid);
        modelMap.put("companyname", novaCustomerInfoService.getById(customerinfoid).getCustomername());
        if (id != null && id != 0) {
            CustomerArea customerAreas = novaCustomerAreaService
                    .findById(id);
            modelMap.put("customerArea", customerAreas);
        }
        return new ModelAndView("system/customerAreaEdit", modelMap);

    }

    @RequestMapping(value = "existcustomerArea")
    @ResponseBody
    public Map<String, Object> existcustomerArea(CustomerArea customerArea) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("existFlag", novaCustomerAreaService.existCustomerArea(customerArea));
        return map;
    }

    /**
     * 确认添加或修改
     */
    @RequestMapping(value = "customerAreaSave")
    public ModelAndView customerAreaSave(HttpServletRequest request,
                                         HttpSession session, CustomerArea customerArea, ModelMap modelMap) {
        if (customerArea.getId() == null || customerArea.getId() == 0) {
            novaCustomerAreaService.insert(customerArea);
        } else {
            novaCustomerAreaService.update(customerArea);
        }
        CustomerArea customerArea1 = new CustomerArea();
        customerArea1.setCustomerinfoid(customerArea.getCustomerinfoid());
        return customerAreaList(request, session, 1, customerArea1, modelMap);
    }

    /**
     * 确认删除
     */
    @RequestMapping(value = "customerAreaDel")
    public ModelAndView customerAreaDel(HttpServletRequest request,
                                        HttpSession session, Integer id, ModelMap modelMap) {
        CustomerArea customerArea = novaCustomerAreaService.findById(id);
        novaCustomerAreaService.delete(id);
        CustomerArea customerArea1 = new CustomerArea();
        customerArea1.setCustomerinfoid(customerArea.getCustomerinfoid());
        return customerAreaList(request, session, 1, customerArea1, modelMap);
    }
}
